﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HSDc.RingleFlow.Kernel.DBPersit.PersitEnum;

namespace HSDc.RingleFlow.Kernel.DBPersit.Dao
{
    internal class HistoryServiceDao
    {
        internal ProcessInstanceDto RetrieveProcessInstance(string guid)
        {
            RingleFlowEntities entity = Util.ConnectionFactory.GetInstance().GetEntities();
            var pidto = from pi in entity.ProcessInstanceDto
                        where pi.ProcessInstance_GUID == guid
                        select pi;
            if (pidto.ToList().Count == 0)
                return null;
            return pidto.First();
        }

        internal ProcessInstanceHistoryDto RetrieveProcessInstanceHistory(string guid)
        {
            RingleFlowEntities entity = Util.ConnectionFactory.GetInstance().GetEntities();
            var pidto = from pi in entity.ProcessInstanceHistoryDto
                        where pi.ProcessInstance_GUID == guid
                        select pi;
            if (pidto.ToList().Count == 0)
                return null;
            return pidto.First();
        }

        internal ProcessDefinitionDto RetrieveProcessDefinition(int id)
        {
            RingleFlowEntities entity = Util.ConnectionFactory.GetInstance().GetEntities();
            var pddto = (from pd in entity.ProcessDefinitionDto
                         where pd.Definition_ID == id
                         select pd).First();
            return pddto;
        }

        internal List<InstanceVariableDto> RetrieveProcessInstanceVariable(string id)
        {
            RingleFlowEntities entity = Util.ConnectionFactory.GetInstance().GetEntities();
            var ivdto = from iv in entity.InstanceVariableDto
                        where (iv.Instance_GUID == id) &&
                                  (iv.Variable_Type == (int)VariableType.ProcessInstance)
                        select iv;
            return ivdto.ToList();
        }

        internal List<InstanceVariableHistoryDto> RetrieveProcessInstanceVariableHistory(string id)
        {
            RingleFlowEntities entity = Util.ConnectionFactory.GetInstance().GetEntities();
            var ivdto = from iv in entity.InstanceVariableHistoryDto
                        where (iv.Instance_GUID == id) &&
                                  (iv.Variable_Type == (int)VariableType.ProcessInstance)
                        select iv;
            return ivdto.ToList();
        }

        internal List<ActivityInstanceDto> RetrieveActivityInstance(int piid)
        {
            RingleFlowEntities entity = Util.ConnectionFactory.GetInstance().GetEntities();
            var aidto = from ai in entity.ActivityInstanceDto
                        where (ai.ActivityInstance_Belong_ProcessInstance == piid)
                        select ai;
            return aidto.ToList();
        }

        internal List<ActivityInstanceHistoryDto> RetrieveActivityInstanceHistory(int piid)
        {
            RingleFlowEntities entity = Util.ConnectionFactory.GetInstance().GetEntities();
            var aidto = from ai in entity.ActivityInstanceHistoryDto
                        where (ai.ActivityInstance_Belong_ProcessInstance == piid)
                        select ai;
            return aidto.ToList();
        }

        internal ProcessActivityDto RetrieveActivity(int acId)
        {
            RingleFlowEntities entity = Util.ConnectionFactory.GetInstance().GetEntities();
            var acdto = (from ac in entity.ProcessActivityDto
                         where ac.Activity_ID == acId
                         select ac).First();
            return acdto;
        }

        internal List<InstanceVariableDto> RetrieveActivityInstanceVariable(string id)
        {
            RingleFlowEntities entity = Util.ConnectionFactory.GetInstance().GetEntities();
            var ivdto = from iv in entity.InstanceVariableDto
                        where (iv.Instance_GUID == id) &&
                                  (iv.Variable_Type == (int)VariableType.ActivityInstance)
                        select iv;
            return ivdto.ToList();
        }

        internal List<InstanceVariableHistoryDto> RetrieveActivityInstanceVariableHistory(string id)
        {
            RingleFlowEntities entity = Util.ConnectionFactory.GetInstance().GetEntities();
            var ivdto = from iv in entity.InstanceVariableHistoryDto
                        where (iv.Instance_GUID == id) &&
                                  (iv.Variable_Type == (int)VariableType.ActivityInstance)
                        select iv;
            return ivdto.ToList();
        }
    }
}
